﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Configuration;
using System.Windows.Forms;

namespace FormBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private const int BtnWidth = 64;

        /// <summary>
        /// 初始化控件的尺寸及位置
        /// </summary>
        private void InitControlsSizeAndPosition()
        {
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            var avgHeight_Left = this.Height / 3;
            var avgHeight_Right = this.Height / 2;
            btnToTop.Height = btnHome.Height = btnToBottom.Height = avgHeight_Left;
            btnToTop.Location = new Point(0, 0);
            btnHome.Location = new Point(0, avgHeight_Left);
            btnToBottom.Location = new Point(0, avgHeight_Left * 2);
            btnMoveUp.Height = btnMoveDown.Height = avgHeight_Right;
            btnMoveUp.Location = new Point(this.Width - BtnWidth, 0);
            btnMoveDown.Location = new Point(btnMoveUp.Location.X, avgHeight_Right);
            tbTabs.Height = this.Height;
            tbTabs.Width = this.Width - BtnWidth * 2;
            tbTabs.Location = new Point(BtnWidth, 0);

            tbTabs.TabPages.RemoveAt(1);
        }

        private void ExecuteJs(string js)
        {
            var tb = tbTabs.SelectedTab;
            if (tb != null)
            {
                var browser = tb.Controls[0] as ExtendedWebBrowser;
                if (browser != null)
                {
                    browser.Navigate(js);
                }
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            InitControlsSizeAndPosition();

            var wbBrowser = new ExtendedWebBrowser();
            wbBrowser.Parent = tbMain;
            wbBrowser.Dock = DockStyle.Fill;
            wbBrowser.Navigate(ConfigurationManager.AppSettings["dizhi"]);
            wbBrowser.NewWindow2 += wbBrowser_NewWindow2;
            wbBrowser.DocumentCompleted += browser_DocumentCompleted;
            wbBrowser.ScriptErrorsSuppressed = true;
        }

        private void wbBrowser_NewWindow2(object sender, NewWindow2EventArgs e)
        {
            TabPage NewTabPage = new TabPage();
            NewTabPage.Text = "加载中...";
            ExtendedWebBrowser NewTabBrowser = new ExtendedWebBrowser()
            {
                Parent = NewTabPage,
                Dock = DockStyle.Fill,
                Tag = NewTabPage,
                ScriptErrorsSuppressed = true
            };

            e.PPDisp = NewTabBrowser.Application;
            NewTabBrowser.NewWindow2 += wbBrowser_NewWindow2;
            NewTabBrowser.DocumentCompleted += browser_DocumentCompleted;

            tbTabs.TabPages.Add(NewTabPage);
            tbTabs.SelectedTab = NewTabPage;
        }


        void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var browser = sender as WebBrowser;
            if (browser != null)
            {
                var title = browser.DocumentTitle;
                var containerTab = browser.Parent as TabPage;
                if (containerTab != null) containerTab.Text = title;
            }
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            var browser = tbTabs.SelectedTab.Controls[0] as WebBrowser;
            if (browser != null) browser.Navigate(ConfigurationManager.AppSettings["dizhi"]);
        }


        private void tbTabs_DoubleClick(object sender, EventArgs e)
        {
            var currentIndex = -1;
            var tab = tbTabs.SelectedTab;
            if (tab != null&&tbTabs.TabPages.Count>1)
            {
                currentIndex = tbTabs.SelectedIndex;
                tbTabs.TabPages.Remove(tab);
            }
            if (currentIndex != -1)
            {
                tbTabs.SelectedTab = tbTabs.TabPages[currentIndex - 1];
            }
        }

        private void btnToTop_Click(object sender, EventArgs e)
        {
            ExecuteJs("javascript:document.documentElement.scrollTop=0;throw new Error(\"just stop method\");");
        }

        private void btnToBottom_Click(object sender, EventArgs e)
        {
            ExecuteJs("javascript:document.documentElement.scrollTop=document.documentElement.scrollHeight;throw new Error(\"just stop method\");");
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            ExecuteJs("javascript:document.documentElement.scrollTop-=400;throw new Error(\"just stop method\");");
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            ExecuteJs("javascript:document.documentElement.scrollTop+=400;throw new Error(\"just stop method\");");
        }


    }
}
